package com.mxpio.erp.equipment.job;

import cn.hutool.core.date.DateUtil;
import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.enums.EquipmentEnums;
import com.mxpio.erp.common.equipment.entity.EquipmentRepair;
import com.mxpio.erp.common.equipment.service.EquipmentRepairService;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//维修工单，暂定每10分钟对停机时长进行一次计算
@Slf4j
@Component
public class EquipmentRepairShutdownTimeOutJob implements Job {

//    private String parameter;
//
//    public void setParameter(String parameter) {
//        this.parameter = parameter;
//    }
//
    @Autowired
    private EquipmentRepairService equipmentRepairService;

    @Override
    @Transactional
    public void execute(JobExecutionContext context) throws JobExecutionException {

//        log.info("维修任务自动验收job执行开始："+ DateUtil.formatDateTime(new Date()));
//        Object params = context.getJobDetail().getJobDataMap().get("params");
//        Integer offSetHour = 24;//默认24小时，可以在任务中配置
//        if (params!=null&& StringUtils.isNumeric(params.toString())){
//            offSetHour = Integer.parseInt(params.toString());
//        }
//        String hoursBefore = DateUtil.formatDateTime(DateUtils.addHours(new Date(),0-offSetHour));
        /**
         * 实时计算维修工单的停机时间
         * 1.工单类型：CI-应急维修工单
         * 2.工单状态：报修时故障停机为“是”，待验收之前
         * 按当前日期时间-工单创建日期时间
         */
        log.info("应急维修工单停机时长定时任务开始："+DateUtil.now());
        Criteria c = Criteria.create().addCriterion("applyHalt",Operator.EQ, CommonEnums.YesNo.YES.getCode() );
        c.addCriterion("bizType",Operator.EQ, "CI" );
        List<EquipmentRepair> list = equipmentRepairService.list(EquipmentRepair.class, c);
        List<Integer> statusList = new ArrayList<>();
        statusList.add(EquipmentEnums.RepairStatus.APPLY.getCode());
        statusList.add(EquipmentEnums.RepairStatus.SEND.getCode());
        statusList.add(EquipmentEnums.RepairStatus.REPAIR.getCode());
        statusList.add(EquipmentEnums.RepairStatus.PAUSE.getCode());
        c.addCriterion("repairStatus",Operator.IN, statusList);
        if (CollectionUtils.isEmpty(list)) {
            log.warn("符合条件的工单数量为0");
            return;
        }
        for (EquipmentRepair equipmentRepair : list){
            Date applyTime = equipmentRepair.getApplyTime();//工单创建时间
            // 获取当前时间
            Date currentTime = new Date();

            // 将两个日期转换为毫秒值
            long applyTimeMillis = applyTime.getTime();
            long currentTimeMillis = currentTime.getTime();

            // 计算时间差（毫秒）
            long diffInMillis = currentTimeMillis - applyTimeMillis;

            // 将时间差转换为分钟
            long diffInMinutes = diffInMillis / (1000 * 60);
            log.info("应急维修工单停机时长定时任务执行，工单【"+equipmentRepair.getBizNo()+"】计算的停机时长:‘"+diffInMinutes+"’分钟。");
            equipmentRepair.setShutdownTime(diffInMinutes);
            equipmentRepairService.update(equipmentRepair);
        }
        log.info("应急维修工单停机时长定时任务结束："+DateUtil.now());

    }
}
